#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include"system.h"
#include "y.tab.h"
#int INTEGER=50,KEYWORD=100;

def CreateNewScopeTableNode():
      t = ScopeTableNode()
      t.scope = 0
      t.next = None
      for i in range(HashSize):
	    t.table[i] = None
      return t

def Scopelookup(name):
      r = ScopeEntry()
      s = ScopeEntry()
      r = None      # Check original code as r stands for both pointer and symentry
      temp = ScopeTableNode()
      temp = Global
      i = 0
      k = name[0]%HashSize
      while temp.next != None:
	    s = temp.table[k]
	    if s!= None:
		  if s.id == name:
			r = s
		  else:
			while s.next != None:
			      s = s.next
			      if s.id == name:
				    r = s
				    break
	    temp = temp.next
      s=temp.table[k]  
      if s!= None:
	    if s.id  == name:
		  r = s
	    else:
		  while s.next != None:
			s = s.next
			if s.id == name:
			      r = s
			      break
      return r


def InsertScopeTable(name,currentScope,typ):
      if Tail.scope == None:
	    print "NewScope : ",currentScope," .\n"
	    Tail.scope = currentScope
      
      elif Tail.scope != currentScope:
	    print "NewScope : ",currentScope," .\n"
	    temp = ScopeTableNode()
	    temp = CreateNewScopeTableNode()
	    temp.scope = currentScope
	    Tail.next = temp
	    Tail = temp
      p = ScopeEntry()
      p.id=name
      p.scope = currentScope
      p.type = typ
      p.next = Tail.table[name[0]%HashSize]
      Tail.table[name[0]%HashSize] =  p 
      return p
      
def InitializeScopeTable():
      Global=CreateNewScopeTableNode()
      Tail=Global
      InsertScopeTable("ACA","GLOBAL","INT")
      InsertScopeTable("BCA","GLOBAL","INT1")
      InsertScopeTable("ACAa","GLOBALa","INTa")
      InsertScopeTable("ACa","GLOBALa","INTA")

def PopScope():
      temp = ScopeTableNode()
      temp = Global
      while temp.next.next != None:
	    temp = temp.next
      temp.next = None
      free(Tail)
      Tail=temp

'''
/*(int main()
{
  InitializeScopeTable();
  
  struct ScopeEntry *p;
  p=Scopelookup("ACAa");
  printf("\n%s--%s--%s\n",p->id,p->scope,p->type);
  PopScope();
  p=Scopelookup("ACAa");
  if(p==NULL)
    printf("Variable not found\n");
  else printf("\n%s--%s--%s\n",p->id,p->scope,p->type);
  
}
/*int main()
{
  printf("You are in.\n");
  InitializeSymTable();  
  printf("Global Node bana.\n");
  InsertSymTable("ABC1",1,1111);//INTEGER);
  printf("ABC1hmm inserted.\n");
  InsertSymTable("ABC2",1,2222);//FLOAT,(void *)"10.910");
  printf("ABC2 inserted.\n");
  InsertSymTable("BBC3",2,3333);//"POINTER TO CHAR",(void *)"asdad");
  printf("BBC3 inserted.\n");
  struct SymEntry *p;
  p=lookup("BBC3");  
  //p=global->next->table[2];
  printf("Searched for BBC3.Tail ka scope : %d\n",tail->scope);
  if(p==NULL)
    printf("Variable BBC3 not found.\n");
  else printf("Variable found.Name : %s, Type : %d , Scope : %d, Value : %d\n",p->id,p->type,p->scope,p->keywordval);
}*/
'''
